#include "Database.h"
#include <stdexcept>

using namespace std;
namespace Records {

Database::Database()
{
	mNextEmployeeNumber = kFirstEmployeeNumber;
}

Database::~Database()
{
}

Employee& Database::addEmployee(std::string inFirstName, std::string inLastName)
{
	Employee theEmployee;
	theEmployee.setFirstName(inFirstName);
	theEmployee.setLastName(inLastName);
	theEmployee.setEmployeeNumber(mNextEmployeeNumber++);
	theEmployee.hire();
	mEmployees.push_back(theEmployee);

	return mEmployees[mEmployees.size() -1];
}

Employee& Database::getEmployee(int inEmployeeNumber)
{
	for (auto iter = mEmployees.begin(); iter != mEmployees.end(); ++iter) {
		if (iter->getEmployeeNumber() == inEmployeeNumber)
			return *iter;
	}
	cerr << "No employee with number" << inEmployeeNumber << endl;
	throw exception();
}

Employee& Database::getEmployee(std::string inFirstName, std::string inLastName)
{
	for (auto iter = mEmployees.begin(); iter != mEmployees.end(); ++iter) {
		if (iter->getFirstName() == inFirstName && iter->getLastName() == inLastName)
			return *iter;
	}
	cerr << "No employee with first name " << inFirstName << "and" << inLastName << endl;
	throw exception();
}

void Database::displayAll() const
{
	for (auto iter = mEmployees.begin(); iter != mEmployees.end(); ++iter) {
		iter->display();
	}
}

void Database::displayCurrent() const
{
	for (auto iter = mEmployees.begin(); iter != mEmployees.end(); ++iter) {
		if (iter->getIsHired())
			iter->display();
	}
}

void Database::displayFormer() const
{
	for (auto iter = mEmployees.begin(); iter != mEmployees.end(); ++iter) {
		if (!iter->getIsHired())
			iter->display();
	}
}

}
